package com.mspbots.core.amqp.receivers;


import com.mspbots.core.teams.mapper.MessagesMapper;
import com.mspbots.core.ticketai.mapper.TicketMapper;
import com.mspbots.core.wise.mapper.TenantExceptionLogMapper;
import com.mspbots.core.wise.mapper.TenantMapper;
import com.mspbots.core.wise.mapper.TenantUserTimeEntryMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.Map;

@Slf4j
@Component
@RequiredArgsConstructor
public class TenantDeleteReceiver {

    private final TicketMapper ticketMapper;
    private final TenantMapper tenantMapper;
    private final MessagesMapper messagesMapper;
    private final TenantUserTimeEntryMapper tenantUserTimeEntryMapper;
    private final TenantExceptionLogMapper tenantExceptionLogMapper;


    @RabbitListener(queues = "tenant.delete")
    public void processMessage(Long tenantId) {
        log.info("tenantId:{}", tenantId);

        Map<String, Object> param = Map.of("tenantId", tenantId);
        this.tenantMapper.deleteTenantOthers(param);
        Long step = 1000L;

        //ticket
        Long ticketMinId = this.ticketMapper.selectMinId(tenantId);
        Long ticketMaxId = this.ticketMapper.selectMaxId(tenantId);
        if (!ObjectUtils.isEmpty(ticketMinId) && !ObjectUtils.isEmpty(ticketMaxId)) {
            while (ticketMinId < ticketMaxId) {
                Long ticketDeleteNum = this.ticketMapper.deleteByMinMaxId(tenantId, ticketMinId, ticketMinId + step);
                if (ticketDeleteNum > 0) {
                    log.debug("delete ticket OK,tenantId:{} ,ticketMinId: {} ,ticketMinId+1000: {}", tenantId, ticketMinId, ticketMinId + step);

                } else {
                    log.info("delete ticket failed,tenantId:{} ,ticketMinId: {} ,ticketMinId+1000: {}", tenantId, ticketMinId, ticketMinId + step);
                }
                ticketMinId = ticketMinId + step;
            }
        } else {
            log.info("Ticket table is empty,tenantId: {}", tenantId);
        }


        //messages
        Long timeEntryMinId = this.tenantUserTimeEntryMapper.selectMinId(tenantId);
        Long timeEntryMaxId = this.tenantUserTimeEntryMapper.selectMaxId(tenantId);
        if (!ObjectUtils.isEmpty(timeEntryMinId) && !ObjectUtils.isEmpty(timeEntryMaxId)) {
            while (timeEntryMinId < timeEntryMaxId) {
                Long timeEntryDeleteNum = this.tenantUserTimeEntryMapper.deleteByMinMaxId(tenantId, timeEntryMinId, timeEntryMinId + step);
                if (timeEntryDeleteNum > 0) {
                    log.debug("delete timeEntry OK,tenantId:{} ,ticketMinId: {} ,ticketMinId+1000: {}", tenantId, timeEntryMinId, timeEntryMinId + step);
                } else {
                    log.info("delete timeEntry failed,tenantId:{} ,ticketMinId: {} ,ticketMinId+1000: {}", tenantId, timeEntryMinId, timeEntryMinId + step);
                }
                timeEntryMinId = timeEntryMinId + step;
            }
        } else {
            log.info("TenantTimeEntry table is empty,tenantId: {}", tenantId);
        }


        //tenant_time_entry
        Long messagesMinId = this.messagesMapper.selectMinId(tenantId);
        Long messagesMaxId = this.messagesMapper.selectMaxId(tenantId);
        if (!ObjectUtils.isEmpty(messagesMinId) && !ObjectUtils.isEmpty(messagesMaxId)) {
            while (messagesMinId < messagesMaxId) {
                Long messagesDeleteNum = this.messagesMapper.deleteByMinMaxId(tenantId, messagesMinId, messagesMinId + step);
                if (messagesDeleteNum > 0) {
                    log.debug("delete messages OK,tenantId:{} ,ticketMinId: {} ,ticketMinId+1000: {}", tenantId, messagesMinId, messagesMinId + step);
                } else {
                    log.info("delete messages failed,tenantId:{} ,ticketMinId: {} ,ticketMinId+1000: {}", tenantId, messagesMinId, messagesMinId + step);
                }
                messagesMinId = messagesMinId + step;
            }
        } else {
            log.info("messages table is empty,tenantId: {}", tenantId);
        }


        //tenant_exception_logs
        Long logMinId = this.tenantExceptionLogMapper.selectMinId(tenantId);
        Long logMaxId = this.tenantExceptionLogMapper.selectMaxId(tenantId);

        if (!ObjectUtils.isEmpty(logMinId) && !ObjectUtils.isEmpty(logMaxId)) {

            while (logMinId < logMaxId) {
                Long LogDeleteNum = this.tenantExceptionLogMapper.deleteByMinMaxId(tenantId, logMinId, logMinId + step);
                if (LogDeleteNum > 0) {
                    log.debug("delete tenantExceptionLog OK,tenantId:{} ,ticketMinId: {} ,ticketMinId+1000: {}", tenantId, logMinId, logMinId + step);
                } else {
                    log.info("delete tenantExceptionLog failed,tenantId:{} ,ticketMinId: {} ,ticketMinId+1000: {}", tenantId, logMinId, logMinId + step);
                }
                logMinId = logMinId + step;
            }
        } else {
            log.info("tenantExceptionLog table is empty,tenantId: {}", tenantId);
        }

        //tenant_ticket_auditor

    }

}
